using System;
using Microsoft.Practices.Unity;
using Unity.Extensions;
using Xunit;

namespace Unity.Extensions
{
    public class InterceptVirtualInterceptionExtensionTest
    {
        [Fact]
        public void ExceptionsAreUnchanged()
        {
            Recorder.Records.Clear();
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<InterceptionExtension>();
            container.Configure<IInterceptionConfiguration>().InterceptVirtual<SpyVirtual>(typeof(SpyVirtual).GetMethod("ThrowsException"),
                                                                                           new RecordingHandler());

            SpyVirtual obj = container.Resolve<SpyVirtual>();

            Assert.Throws<Exception>(obj.ThrowsException);
            Assert.Equal(3, Recorder.Records.Count);
            Assert.Equal("Before Method", Recorder.Records[0]);
            Assert.Equal("In Method", Recorder.Records[1]);
            Assert.Equal("After Method", Recorder.Records[2]);
        }

        [Fact]
        public void InterceptViaAttributes()
        {
            Recorder.Records.Clear();
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<InterceptionExtension>();

            SpyVirtualAttributes obj = container.Resolve<SpyVirtualAttributes>();
            obj.InterceptedMethod();

            Assert.Equal(3, Recorder.Records.Count);
            Assert.Equal("Before Method", Recorder.Records[0]);
            Assert.Equal("In Method", Recorder.Records[1]);
            Assert.Equal("After Method", Recorder.Records[2]);
        }

        [Fact]
        public void InterceptViaCode()
        {
            Recorder.Records.Clear();
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<InterceptionExtension>();
            container.Configure<IInterceptionConfiguration>().InterceptVirtual<SpyVirtual>(typeof(SpyVirtual).GetMethod("InterceptedMethod"),
                                                                                           new RecordingHandler());

            SpyVirtual obj = container.Resolve<SpyVirtual>();
            obj.InterceptedMethod();
            obj.NonInterceptedMethod();

            Assert.Equal(4, Recorder.Records.Count);
            Assert.Equal("Before Method", Recorder.Records[0]);
            Assert.Equal("In Method", Recorder.Records[1]);
            Assert.Equal("After Method", Recorder.Records[2]);
            Assert.Equal("In Non-Intercepted Method", Recorder.Records[3]);
        }

        public class SpyVirtual
        {
            public virtual void InterceptedMethod()
            {
                Recorder.Records.Add("In Method");
            }

            public void NonInterceptedMethod()
            {
                Recorder.Records.Add("In Non-Intercepted Method");
            }

            public virtual void ThrowsException()
            {
                Recorder.Records.Add("In Method");
                throw new Exception("This is my exception!");
            }
        }

        public class SpyVirtualAttributes
        {
            [VirtualIntercept(typeof(RecordingHandler))]
            public virtual void InterceptedMethod()
            {
                Recorder.Records.Add("In Method");
            }
        }
    }
}